NERR Cluster

nerr_h_comp<-read.csv('data/WQ-NUT_overallMedians.csv')
head(nerr_h_comp)
##   X station   Station.Name Reserve.Code                  Reserve.Name State
## 1 1   acebb        Big Bay          ACE Ashepoo Combahee Edisto Basin    SC
## 2 2   aceei  Edisto Island          ACE Ashepoo Combahee Edisto Basin    SC
## 3 3   acefc  Fishing Creek          ACE Ashepoo Combahee Edisto Basin    SC
## 4 4   acemc Mosquito Creek          ACE Ashepoo Combahee Edisto Basin    SC
## 5 5   acesp     St. Pierre          ACE Ashepoo Combahee Edisto Basin    SC
## 6 6   apacp      Cat Point          APA              Apalachicola Bay    FL
##   Latitude Longitude yr_start_wq yr_end_wq yr_start_nut yr_end_nut temp_median
## 1 32.49410  80.32410        2002      2015         2002       2014       21.90
## 2 32.50400  80.32470        2015      2022         2015       2022       22.05
## 3 32.63593  80.36556        2002      2022         2002       2022       21.25
## 4 32.55580  80.43800        2002      2022         2002       2022       21.60
## 5 32.52800  80.36144        2002      2022         2002       2022       21.90
## 6 29.70210  84.88020        2002      2022         2002       2022       23.75
##   spcond_median sal_median do_mgl_median do_pct_median ph_median turb_median
## 1       47.7800      31.10           6.2        84.525       7.8          19
## 2       44.7400      28.90           6.1        80.700       7.7          17
## 3       11.1025       6.30           5.3        63.900       6.9          28
## 4       28.3100      17.40           5.9        73.300       7.4          24
## 5       45.1475      29.25           6.0        81.150       7.7          17
## 6       35.3100      22.30           7.0        93.525       8.0           8
##   temp_nPoints spcond_nPoints sal_nPoints do_mgl_nPoints do_pct_nPoints
## 1       342706         324398      324398         280781         296626
## 2       272515         260624      260624         271318         272419
## 3       612647         601919      601919         560211         560214
## 4       589475         579229      579229         533877         534111
## 5       601172         586247      586256         543518         546812
## 6       599913         556305      556305         546218         546199
##   ph_nPoints turb_nPoints temp_nMonths spcond_nMonths sal_nMonths
## 1     307775       345100          156            152         152
## 2     249262       247825           96             95          95
## 3     537181       563414          240            240         240
## 4     518378       553330          240            235         235
## 5     513352       574413          250            248         248
## 6     516729       556902          248            245         245
##   do_mgl_nMonths do_pct_nMonths ph_nMonths turb_nMonths chla_nMonths
## 1            145            150        146          155          145
## 2             96             96         94           94           78
## 3            235            235        229          235          220
## 4            233            233        226          237          222
## 5            239            239        233          247          225
## 6            246            246        240          246          226
##   nh4f_nMonths no23_nMonths po4f_nMonths    chla     nh4f      no23f     po4f
## 1          155          153          152 4.66300 0.047450 0.01525000 0.043125
## 2           78           72           82 6.82000 0.024425 0.02487500 0.025625
## 3          237          223          232 8.07750 0.051500 0.03945000 0.071600
## 4          234          226          232 7.53175 0.043575 0.03550000 0.053975
## 5          236          223          235 4.03400 0.052175 0.01675000 0.038500
## 6          226          224          225 6.15000 0.023500 0.04316667 0.002000
colnames(nerr_h_comp)
##  [1] "X"              "station"        "Station.Name"   "Reserve.Code"  
##  [5] "Reserve.Name"   "State"          "Latitude"       "Longitude"     
##  [9] "yr_start_wq"    "yr_end_wq"      "yr_start_nut"   "yr_end_nut"    
## [13] "temp_median"    "spcond_median"  "sal_median"     "do_mgl_median" 
## [17] "do_pct_median"  "ph_median"      "turb_median"    "temp_nPoints"  
## [21] "spcond_nPoints" "sal_nPoints"    "do_mgl_nPoints" "do_pct_nPoints"
## [25] "ph_nPoints"     "turb_nPoints"   "temp_nMonths"   "spcond_nMonths"
## [29] "sal_nMonths"    "do_mgl_nMonths" "do_pct_nMonths" "ph_nMonths"    
## [33] "turb_nMonths"   "chla_nMonths"   "nh4f_nMonths"   "no23_nMonths"  
## [37] "po4f_nMonths"   "chla"           "nh4f"           "no23f"         
## [41] "po4f"
nerr_h_comp$L10_turb <- log10(nerr_h_comp$turb_median)
nerr_h_comp$L10_po4f <- log10(nerr_h_comp$po4f)
nerr_h_comp$L10_no23f <- log10(nerr_h_comp$no23f)
nerr_h_comp$L10_nh4f <- log10(nerr_h_comp$nh4f)
nerr_h_comp$L10_chla <- log10(nerr_h_comp$chla)
nerr_h_comp <- nerr_h_comp %>% 
         dplyr::select(station,spcond_median, temp_median, do_mgl_median, ph_median, L10_turb, L10_chla, L10_nh4f, L10_no23f,L10_po4f)
nerr_h_comp <- nerr_h_comp %>% 
       rename(code = 'station',Temp='temp_median',SpCond='spcond_median',Turb='L10_turb',DO='do_mgl_median',NO23='L10_no23f',
              NH4='L10_nh4f',PO4='L10_po4f',Chla='L10_chla',pH=ph_median)

colnames(nerr_h_comp)
##  [1] "code"   "SpCond" "Temp"   "DO"     "pH"     "Turb"   "Chla"   "NH4"   
##  [9] "NO23"   "PO4"
pca_nerr = prcomp(nerr_h_comp[,2:ncol(nerr_h_comp)], center = TRUE, scale = TRUE)
pca_nerr
## Standard deviations (1, .., p=9):
## [1] 1.7068909 1.5262369 1.2014663 0.9311692 0.7962843 0.6342399 0.4307805
## [8] 0.3660578 0.3010445
## 
## Rotation (n x k) = (9 x 9):
##                PC1         PC2          PC3          PC4           PC5
## SpCond -0.47585553 -0.10562380 -0.369142363 -0.141788252 -0.1013437331
## Temp   -0.14102731 -0.57419888  0.232807536 -0.002267199 -0.1173393145
## DO      0.07171512  0.57662754  0.180686050 -0.324798789 -0.0164644283
## pH     -0.40576069  0.08797909 -0.138698800 -0.619283992 -0.2341459091
## Turb    0.43548332 -0.32300071 -0.008433355 -0.216721723  0.2498698202
## Chla    0.29325701 -0.32421987  0.109959369 -0.639363999  0.0005773586
## NH4     0.30422790 -0.13142462 -0.469706271  0.137706574 -0.7199640464
## NO23    0.45082453  0.30293329 -0.037769522 -0.106625058 -0.2392896835
## PO4     0.11155828  0.02026380 -0.723489502 -0.069263298  0.5318105938
##                PC6         PC7         PC8         PC9
## SpCond -0.24626090  0.01484365  0.69135209 -0.23860450
## Temp    0.31736983  0.11528470 -0.19409288 -0.65631374
## DO     -0.17543755 -0.38813613 -0.08889209 -0.57845920
## pH      0.47681893 -0.07602463 -0.22961813  0.29014323
## Turb    0.31688328 -0.58116855  0.39028419  0.07782562
## Chla   -0.53367196  0.29780324 -0.08817841  0.08207387
## NH4    -0.13910680 -0.26781135 -0.19794148 -0.04453308
## NO23    0.41239471  0.55782809  0.36513120 -0.14272809
## PO4     0.07652751  0.14394777 -0.30188600 -0.24160832
pca_nerr$rotation
##                PC1         PC2          PC3          PC4           PC5
## SpCond -0.47585553 -0.10562380 -0.369142363 -0.141788252 -0.1013437331
## Temp   -0.14102731 -0.57419888  0.232807536 -0.002267199 -0.1173393145
## DO      0.07171512  0.57662754  0.180686050 -0.324798789 -0.0164644283
## pH     -0.40576069  0.08797909 -0.138698800 -0.619283992 -0.2341459091
## Turb    0.43548332 -0.32300071 -0.008433355 -0.216721723  0.2498698202
## Chla    0.29325701 -0.32421987  0.109959369 -0.639363999  0.0005773586
## NH4     0.30422790 -0.13142462 -0.469706271  0.137706574 -0.7199640464
## NO23    0.45082453  0.30293329 -0.037769522 -0.106625058 -0.2392896835
## PO4     0.11155828  0.02026380 -0.723489502 -0.069263298  0.5318105938
##                PC6         PC7         PC8         PC9
## SpCond -0.24626090  0.01484365  0.69135209 -0.23860450
## Temp    0.31736983  0.11528470 -0.19409288 -0.65631374
## DO     -0.17543755 -0.38813613 -0.08889209 -0.57845920
## pH      0.47681893 -0.07602463 -0.22961813  0.29014323
## Turb    0.31688328 -0.58116855  0.39028419  0.07782562
## Chla   -0.53367196  0.29780324 -0.08817841  0.08207387
## NH4    -0.13910680 -0.26781135 -0.19794148 -0.04453308
## NO23    0.41239471  0.55782809  0.36513120 -0.14272809
## PO4     0.07652751  0.14394777 -0.30188600 -0.24160832
pca_nerr$x
##                 PC1           PC2          PC3          PC4          PC5
##   [1,] -0.200402235 -1.3716284766 -1.156218215 -0.319546336  0.218042300
##   [2,] -0.101213563 -1.3688465409 -0.407624370 -0.435015947  0.474825539
##   [3,]  2.251106345 -1.8307450578 -0.576056899  1.093229014  1.122228614
##   [4,]  1.130268086 -1.5347449068 -0.749682819  0.097642905  0.688009239
##   [5,] -0.090838185 -1.3742605387 -1.092429822  0.042622516  0.136904731
##   [6,] -0.668846285 -0.7542562318  1.336204721 -0.719092436 -1.032696555
##   [7,] -0.633027479 -0.7532834813  1.675570901 -1.053931619 -0.570004705
##   [8,]  0.587244930 -1.3408750890  1.893257460 -0.420240900 -0.659889430
##   [9,]  0.456143686 -1.2600239113  2.019446114 -0.066713981 -0.568200924
##  [10,] -1.556844449 -0.8599200287  1.565632267 -0.822383041 -0.275651020
##  [11,]  2.231707491  0.9245124975 -0.322147025  0.847785325  0.380157846
##  [12,]  0.993945443  0.4138394685  1.277215629  0.385101977  1.657726094
##  [13,]  2.996807469 -0.4093413434  0.620555242  1.530779719  0.532089229
##  [14,]  2.578889729 -0.0126943829  1.720134459 -1.638732511 -0.643327845
##  [15,]  2.972013063  0.4733720203  0.427812743  0.211414585  0.056120173
##  [16,]  0.202135191 -0.1437396170  0.299160519 -0.861173464  0.103111402
##  [17,] -1.317250949  0.6364755584  0.559372478 -0.820208903 -0.499045491
##  [18,]  2.504200708 -0.0420175524  0.668102102  0.396502786  0.749849857
##  [19,]  2.650104405  0.0333424818  0.418241010  0.299140842  0.611537175
##  [20,]  1.779428554 -0.6287036473 -0.230970450 -0.689960289  0.572103437
##  [21,]  3.500890689 -0.2477542409  0.757957832 -0.601802078  0.432152438
##  [22,]  3.191791717  0.0782592771  0.685929154  0.078244142 -0.006123801
##  [23,]  3.763380186 -0.0760233949 -0.058890355 -0.158826102 -0.172941476
##  [24,]  3.280975587 -0.4972977652 -1.043869017  0.148998216 -0.169150158
##  [25,] -0.682143036 -0.3024112143 -1.779002581 -0.112699864 -0.125511431
##  [26,] -0.442781406 -0.3122467814 -1.715660920 -0.768751512 -0.602418359
##  [27,] -0.361898273  0.2507669022 -1.672341817 -0.425375281 -0.531479672
##  [28,] -0.469112447  1.2469059008 -1.548993174 -0.348276211 -0.638444749
##  [29,] -0.403608782 -1.4954651152  1.565020771  0.488041905  0.702323319
##  [30,]  0.008793802 -2.4950773359  0.743109306  1.863872253  0.066027246
##  [31,] -1.084534932 -0.9989652274  1.734490439 -0.465565086  1.035095218
##  [32,] -1.473759318 -0.8508444123  1.599950598 -1.280485174  0.811326546
##  [33,] -0.450560532  1.1217288344 -0.372265258 -0.712758740 -0.029546294
##  [34,]  0.668614015  1.1084910135  0.523134638  0.920650583 -0.353576626
##  [35,]  0.570130570  0.5241760111 -0.974704554 -0.311168462 -0.195713944
##  [36,]  1.239618709  0.2009062917 -1.255586508 -0.407288408 -0.754204112
##  [37,] -1.444502463 -1.1671233889 -0.372951792 -0.252464884 -0.349625437
##  [38,]  0.113926855 -1.9020131198 -0.180164217  0.598238574  0.588820774
##  [39,] -0.446348680 -1.6793025968 -0.469757162  0.265813800  0.088469670
##  [40,] -0.995893112 -1.3872279768 -0.483224963 -0.240095207 -0.281683631
##  [41,] -3.027964976 -0.2234141996  0.721362143  0.801235308 -0.633435456
##  [42,] -3.835626745 -0.0137166576  1.624765921  0.697734421  0.794234857
##  [43,] -0.158808712 -2.6395082069  1.088743734  3.418297230  1.917929516
##  [44,]  1.977975795  2.1824657315 -0.103368017 -0.210896570  0.337608506
##  [45,]  0.592960851  2.2460243461 -0.145075515  0.415554334 -0.071304835
##  [46,] -0.311428188  3.2497037510  1.471969521 -0.287988139 -0.461674468
##  [47,]  1.620985986  1.1036946924  0.826206821  0.280418832 -0.198529765
##  [48,]  1.373952902  1.3364978751  0.848868152  0.160140710 -0.184280539
##  [49,] -1.093629908  0.3834892787 -0.291625481 -0.974116310  0.744920454
##  [50,] -0.969655799  0.2534052668 -0.190459330 -0.807629787  0.729378247
##  [51,]  2.327756907  0.8003908611  1.879419408  2.266133651  1.188670257
##  [52,]  0.773992992  0.8162425813  0.006019797  0.479491312  0.081344354
##  [53,] -2.225347032 -2.4733204839  0.154754492  1.223835897 -1.511682740
##  [54,] -2.783970485 -1.5595089404  0.735263984  1.555247813 -2.261474309
##  [55,] -3.589013613 -0.7308212872  0.932792928  1.447992394 -2.114486266
##  [56,] -3.377015091 -0.7617413519  0.693250289  1.705113946 -1.966007942
##  [57,] -1.098870382  3.5338504324 -1.081945910 -0.130526372  0.062650001
##  [58,] -1.068722248  3.4291910363 -0.497705539 -0.829821368  0.275441956
##  [59,] -1.432547065  3.6603403021 -0.898474939 -0.110579070  0.025737292
##  [60,] -1.293086085  3.7749664712 -0.804811104 -0.360103220 -0.079252269
##  [61,]  1.820782933  1.1043111257  1.015708140 -0.636520991 -0.663248537
##  [62,]  1.716814118  1.0308810119  0.411319244 -0.137532040 -1.434833366
##  [63,]  0.687219935  0.9388947705  1.453922063 -0.119340969 -0.075809400
##  [64,]  1.738179687 -0.8672086429  0.545401703  0.369344054  1.213091293
##  [65,] -2.047181955 -0.8432391422  0.849468340 -1.376572855  1.507255639
##  [66,] -1.491216160 -0.9825672954  0.457046271 -1.268093769  1.789807668
##  [67,] -1.210164987 -0.9596908541  0.662321087 -1.439722041  2.219623981
##  [68,] -1.641186601 -1.0075826426  0.849343395 -1.597527957  1.887088237
##  [69,] -2.360956882 -0.8693036050  1.141307019 -0.985282761  0.843374489
##  [70,] -1.547947533  0.5694738280 -0.594717247  1.162334194  0.664706741
##  [71,] -2.054506895  1.8437973242 -0.421696017 -0.029493929  0.454390342
##  [72,] -2.450808989  1.9074444487 -0.350663902  0.033744764  0.480026709
##  [73,] -2.615856736  1.8977266476 -0.061879094  0.015686479  0.720067705
##  [74,] -0.698045232 -1.2542467284 -0.554445303  0.039682384 -0.326488311
##  [75,] -0.754891838 -1.2400737082 -0.429912888 -0.042880265 -0.310582498
##  [76,] -0.481223384 -1.6568091126 -0.571446610 -0.006497154 -0.192064821
##  [77,]  2.558238550 -1.0424716569 -0.427627827  0.486928124  0.108041894
##  [78,]  0.386470146 -0.3471305811 -0.256096038 -0.679098947 -0.536365658
##  [79,] -1.938679135 -0.4597308466  0.447786452  0.041224615 -0.940505437
##  [80,] -1.571049075 -0.7421068599  0.469273049 -0.004676726 -0.818362895
##  [81,]  0.698492042 -0.9579087491 -0.593217851 -0.622341584 -0.176788790
##  [82,]  1.624233260  1.7798689279  0.043418886  0.078660892 -0.134865204
##  [83,]  2.176853333  0.6127418112  0.173218811  0.170585917 -0.133478985
##  [84,]  2.893275800  0.0806549771  0.486302312 -1.253010841 -0.799826228
##  [85,]  2.416996108  0.8357356176 -0.098232297 -0.604840512 -0.526701751
##  [86,]  2.858120641  0.0763196535  0.439038875 -1.146151559 -0.866330538
##  [87,] -0.984400494  2.7255460689 -1.012165889 -0.460020248  0.315200326
##  [88,] -0.777211751  2.5177786990 -1.123760966 -0.245997442  0.458817505
##  [89,] -0.693563623  2.0760260059 -1.502783625  0.455165336  0.442330872
##  [90,] -1.169314595  2.7905099635 -0.947255306 -0.106219366  0.474282792
##  [91,]  1.732591485  1.0815550088 -1.901958309  0.175116312 -1.990578094
##  [92,]  3.649191948  0.0991618026 -1.231637170  1.956854580 -1.204831994
##  [93,] -1.244916810 -2.1504356005 -0.024574979  0.142930733 -0.054188461
##  [94,] -1.002282591 -1.8278480814  0.020435420  0.770764334 -0.358235807
##  [95,] -1.568654689 -1.9758843644  0.251618881 -0.006462161 -0.226801785
##  [96,] -0.877969904 -2.3379779556 -0.773824321  0.560543518 -0.053605710
##  [97,] -1.182250463 -2.2478339755 -0.330734031  0.257924885  0.182840392
##  [98,] -0.483619842 -1.7425300497 -1.185686907 -0.693910238  0.514012127
##  [99,]  0.845615004 -2.0875022681 -2.817962762  0.924274794 -0.049245679
## [100,] -0.048343429 -2.1371767093 -0.322268437  0.811767158  0.894031818
## [101,] -0.587368078 -1.8086869519 -0.580721312  0.836763536  0.550413107
## [102,]  0.250655950 -1.7188864664 -0.596870964 -0.638490781  0.403688528
## [103,]  0.279269891 -1.2068536914 -0.511648116 -0.718464790  0.400509068
## [104,]  1.438860748  0.0007382128 -1.482703865 -1.156823833  0.578599427
## [105,]  2.720853435  0.0579177824 -1.687304620 -0.216494783  0.663302905
## [106,]  1.494884432 -0.3796501178 -1.830187439 -0.608209909  0.480096061
## [107,]  2.377565841  0.2199104730 -0.796701983 -0.010000666  0.563415423
## [108,] -0.712774538  2.0242653299 -1.075755760 -0.451663010 -0.249650202
## [109,] -0.371311352  1.6944390827 -0.886390077 -0.397624467 -0.256006594
## [110,]  0.655317902  1.8903871660  0.393545938  1.207170902  0.015825133
## [111,] -1.191217704  0.4490428537 -2.438682619  0.630712273 -0.548877125
## [112,] -0.294420241 -0.7746409245 -2.517613173  1.088253255 -0.619797861
## [113,] -0.504620967 -0.7169046667 -2.745025510  1.017599068 -0.133502686
## [114,] -2.037601031 -0.8989471892 -0.397771498 -2.229720409  0.303929686
## [115,] -1.392618397 -0.7707606542 -0.935645870 -0.113985431  0.233852377
## [116,] -1.522184879 -0.5081826534 -1.006476515 -0.095942061  0.416729401
## [117,]  0.243056712 -1.3522783207 -2.871464992  1.025608844 -0.088930048
## [118,] -0.242637192  3.8072064452  3.520601285  3.013532431  0.826966318
## [119,] -1.234472058  2.5472987285 -0.584276784  0.300969308 -0.268877907
## [120,] -1.051500395  2.3530117797  0.837842678  0.778126484 -0.598463442
## [121,] -0.385992363  2.7746617263  3.165889291  1.989589410  0.771261839
## [122,]  1.993994069 -1.0443822850  2.202013244 -0.060165184 -1.467970112
## [123,]  0.893527003 -0.9821496223  2.441104843 -1.835751697 -1.144161720
## [124,]  1.452017254 -1.2269680811  2.254531908 -1.364491169 -1.422601647
## [125,]  0.732746280 -1.1219639011  2.305809670 -1.760925100 -1.203000518
## [126,] -0.499559924  0.5571001065  0.282048563 -1.005055174 -0.524935227
## [127,] -1.873865792  1.6720270140 -0.144223833 -0.363595015  0.253052017
## [128,] -2.360899858  1.1953656281  0.355591662 -0.178246210 -0.170433028
## [129,] -2.427026348  0.7525405535  0.550395858  0.250927746 -0.303155786
##                  PC6          PC7          PC8          PC9
##   [1,]  0.2392498507 -0.431464195  0.243083289 -0.143778968
##   [2,]  0.0930351866  0.064082296  0.474974308 -0.043481087
##   [3,] -0.3297019194  0.203855434 -0.274567373 -0.050483163
##   [4,]  0.0092686648  0.015856104  0.004064807 -0.102713767
##   [5,]  0.2153417873 -0.353444892  0.219941728 -0.120149893
##   [6,]  0.3699533749  0.075365616  0.296060158  0.122846968
##   [7,]  0.3269294711  0.065908657  0.372795039  0.122537436
##   [8,] -0.2110245161 -0.136086896 -0.248752470  0.091201448
##   [9,] -0.3678170434  0.290597114 -0.379704108  0.128224046
##  [10,]  0.3247920944 -0.066753603  0.420484635  0.458473364
##  [11,]  0.2466552490  0.085466546 -0.586730686 -0.410147482
##  [12,] -1.0403413482  0.480448719  0.374139656 -0.019014646
##  [13,] -0.7604364704  0.337967478  0.140801807 -0.066748291
##  [14,] -0.0863594906 -0.016400593 -0.140875914 -0.284545975
##  [15,] -0.2597606938  0.104099570 -0.009171909 -0.114953452
##  [16,] -0.4826314145 -0.610303794 -0.307319299  0.197642123
##  [17,] -0.8238042703 -1.180281514 -0.931727079  0.208814590
##  [18,]  0.0835278239 -0.307791962 -0.003715411 -0.210905512
##  [19,] -0.0467292310 -0.368538423 -0.151252782 -0.387197932
##  [20,] -0.2357731431 -0.765922520 -0.021753087  0.091016736
##  [21,] -0.9080652565  0.016549022  0.220428858 -0.116778346
##  [22,] -1.7583953440  0.520258350 -0.480681106 -0.429661589
##  [23,] -0.6177556203  0.066548837  0.128470499 -0.271833568
##  [24,] -0.3626023411  0.027936473  0.422007870  0.083213092
##  [25,] -0.2363021838  0.273025291 -0.083871364  0.342036656
##  [26,]  0.0245055087  0.392842305  0.001067156  0.414587306
##  [27,]  0.1411209072  0.198831194  0.189257985  0.164451241
##  [28,]  0.0782455331  0.169054972  0.252350249 -0.004879045
##  [29,] -0.6987440644  0.187157676  0.010710289 -0.004550123
##  [30,] -0.9154359690  0.588713567  0.158470008  0.754220748
##  [31,] -0.1552617317 -0.068600012  0.184566469  0.023598479
##  [32,]  0.3262028363 -0.346872756  0.067777942  0.132888248
##  [33,] -0.3998936109  0.139609643 -0.082952314 -0.062177706
##  [34,] -0.2534683819 -0.112144925 -0.032414098 -0.565775018
##  [35,] -0.1448822629  0.003430294 -0.005985788 -0.232031620
##  [36,] -0.1115730876 -0.391140899 -0.099628518 -0.220107936
##  [37,]  0.2746789109 -0.136563793  0.084489562 -0.172922177
##  [38,] -0.2716641949  0.426367926 -0.627291885 -0.082411245
##  [39,] -0.0650790848 -0.018433397  0.060103306 -0.277590821
##  [40,]  0.2802813038  0.042763696  0.267851038 -0.177241128
##  [41,]  0.9903786349  0.119603602  0.059365786 -0.349507591
##  [42,]  1.2413789743  0.433232550  0.420356174 -0.224145026
##  [43,]  0.7048557087  0.052311898 -0.537627597  1.277060636
##  [44,]  1.2860802893 -0.082911395 -0.162389145 -0.203109278
##  [45,]  1.3950750935  0.125959302 -1.104437000 -0.222371692
##  [46,]  1.5641330689  0.472775136 -0.795967616 -0.048462006
##  [47,]  0.5431145699 -0.171251346 -0.362538921 -0.095115785
##  [48,]  0.6445203383 -0.097395193 -0.461212843 -0.061114764
##  [49,] -0.4485934288 -0.808374372  0.030870783  0.199614089
##  [50,] -0.5390239381 -0.401496407 -0.050106311  0.084900374
##  [51,] -1.0015808045 -0.127722538  0.697821830 -0.682471650
##  [52,] -0.8160710080 -0.155745862 -0.117644982 -0.321917643
##  [53,] -0.3666827957  0.580879640 -0.175277198 -0.096449171
##  [54,] -0.0818333791  0.517384290 -0.165851871 -0.342431659
##  [55,]  0.5812070018 -0.237558775 -0.371068372 -0.455760605
##  [56,]  1.1521921126 -0.762264542 -0.070057528 -0.433412906
##  [57,] -0.2107213676 -0.208725761  0.397899621  0.165854092
##  [58,] -0.0813149023 -0.515793955  0.320914079 -0.246026509
##  [59,] -0.4108050584  0.391909616  0.213276975  0.101437682
##  [60,] -0.6427138804  0.280055612  0.130122481 -0.024823999
##  [61,] -0.3299351779  0.049103220 -0.616403135  0.143983021
##  [62,]  0.0969336915 -0.485416847 -0.917787378  0.004065495
##  [63,]  0.3180959780 -0.211413159 -0.799794549  0.027767866
##  [64,]  0.5203375563 -0.880039988 -0.128112884  0.988547823
##  [65,]  0.6006642660  0.170412457  0.055793989 -0.088719728
##  [66,]  0.6519682665  0.096914484 -0.392543412 -0.365221745
##  [67,]  0.8486719776 -0.168738914 -0.249780677 -0.180333801
##  [68,]  0.8329573703 -0.133428901  0.081584658 -0.077202887
##  [69,]  0.6104804931  0.130047710  0.289910652  0.077874068
##  [70,] -0.9870894175 -0.052370758 -0.335478832  0.102207803
##  [71,] -1.2441672413  0.056851902 -0.659308206  0.210714116
##  [72,] -1.0373635880 -0.069617600 -0.548428617  0.451409063
##  [73,] -0.9259596761 -0.008872934 -0.514781883  0.376222366
##  [74,] -0.4806714451 -0.424620168  0.361942577  0.108782469
##  [75,] -0.5994892144 -0.392281149  0.270559201  0.052547648
##  [76,] -0.7598404011 -0.292912323  0.341996371  0.103525045
##  [77,] -0.1360408442 -0.184348349 -0.139487794 -0.394970617
##  [78,]  0.0346775450  0.104948143  0.082021124 -0.230369975
##  [79,] -0.3342552482 -0.963995687  0.097587075  0.258109143
##  [80,] -0.5384303559 -0.890647392  0.082360843  0.301592034
##  [81,] -0.0563657415  0.104850720  0.105609932 -0.100222689
##  [82,]  1.6239557813  0.466659100 -0.205317300  0.195790552
##  [83,]  1.2541423247  0.659001771  0.111934643  0.671430869
##  [84,]  0.3901378952  0.124671721 -0.083234777  0.443341630
##  [85,]  1.2059701150  0.600575985 -0.105598155  0.548866245
##  [86,]  0.4091838079  0.035320734 -0.102067547  0.477694702
##  [87,] -0.0862789273  0.302271704  0.006025155 -0.275812563
##  [88,] -0.0900433029  0.746234951  0.238018641 -0.007425653
##  [89,]  0.1791089101  1.185261227  0.501457598  0.516937117
##  [90,] -0.1654530017  1.273856514  0.070815768 -0.113420851
##  [91,] -0.1959503266 -0.654984708  0.721974180 -0.051482623
##  [92,] -0.0932412900 -0.379367131  0.367403945  0.938153940
##  [93,]  0.2142611072  0.169126983 -0.079940173 -0.117174646
##  [94,]  0.6307217743  0.379044936  0.101585570 -0.155057018
##  [95,]  0.3780769978  0.099039349  0.216846276 -0.091839421
##  [96,]  0.5580800269  0.447262590  0.278484394 -0.131816925
##  [97,]  0.4672611800  0.135636174 -0.017000552 -0.084374404
##  [98,] -0.5171184392 -0.661254728 -0.292953682 -0.243090381
##  [99,] -0.0700958499 -0.154662676 -0.415296187 -0.398627015
## [100,] -0.7037919648  0.492580677 -0.214130729 -0.369111135
## [101,] -0.6963295917  0.045432992 -0.355494676  0.008350379
## [102,] -0.1093075402 -0.316409482  0.284131500  0.054369594
## [103,]  0.0364358326 -0.021875265  0.210264629 -0.414195733
## [104,]  0.5771080352 -0.498795695  0.600636040 -0.209537111
## [105,]  1.3822507376  0.102837607 -0.301745224 -0.155782483
## [106,]  0.5129625786 -0.502233390  0.331532280 -0.108776138
## [107,]  1.0023656570 -0.240006067 -0.132828934 -0.140808724
## [108,] -0.0740488207  0.133813665  0.216851065 -0.049088869
## [109,] -0.1525464010  0.064872773  0.289401300 -0.012300609
## [110,]  0.1040341264 -0.314776326  0.709182614 -0.027464618
## [111,]  0.3459221986 -0.147885906 -0.525238791 -0.174584497
## [112,]  0.3461436254 -0.462793372  0.295832072  0.169626253
## [113,]  0.1990377465  0.306644588 -0.248957426  0.003301457
## [114,] -0.6849441153  0.748731152  0.387940456 -0.009710346
## [115,]  0.4789851038 -0.290454689  0.251462381  0.032963609
## [116,]  0.4769377292 -0.139210048  0.123201257 -0.134130214
## [117,]  0.5568338594  0.854690706  0.064946521  0.128384416
## [118,]  0.1167063560 -0.393587273  0.620452262 -0.520433582
## [119,] -0.2499983431 -0.720699482  0.482604381  0.254303757
## [120,]  0.2981387726 -0.510146306  0.492883910  0.254054080
## [121,]  0.6009306951 -0.289032488  0.776777268  0.186862716
## [122,] -0.4449584306  0.734541125  0.492492679 -0.207546157
## [123,] -0.0517613842  0.297079618  0.115591871  0.130199523
## [124,]  0.0005872656  0.522569798  0.359778541  0.024064282
## [125,] -0.2529304562  0.043739373  0.029471724  0.191360297
## [126,] -1.1701690753  1.191552982  0.066937436  0.079912269
## [127,] -0.7478348228 -0.248449075 -0.138836979  0.198553188
## [128,] -0.9103565477  0.193351617 -0.548310100  0.013560909
## [129,] -0.9581814591  0.164933125 -0.539375991  0.125784832
## rotate the pca (arbitrary, but wanted to conform visualizations with previous work)
pca_nerr$rotation[, c(2,3)] <- pca_nerr$rotation[, c(2,3)] * -1
pca_nerr$x[, c(2,3)] <- pca_nerr$x[, c(2,3)] * -1
pca_nerr$
pca_nerr
## NULL
pca_nerr$rotation
##                PC1         PC2          PC3          PC4           PC5
## SpCond -0.47585553  0.10562380  0.369142363 -0.141788252 -0.1013437331
## Temp   -0.14102731  0.57419888 -0.232807536 -0.002267199 -0.1173393145
## DO      0.07171512 -0.57662754 -0.180686050 -0.324798789 -0.0164644283
## pH     -0.40576069 -0.08797909  0.138698800 -0.619283992 -0.2341459091
## Turb    0.43548332  0.32300071  0.008433355 -0.216721723  0.2498698202
## Chla    0.29325701  0.32421987 -0.109959369 -0.639363999  0.0005773586
## NH4     0.30422790  0.13142462  0.469706271  0.137706574 -0.7199640464
## NO23    0.45082453 -0.30293329  0.037769522 -0.106625058 -0.2392896835
## PO4     0.11155828 -0.02026380  0.723489502 -0.069263298  0.5318105938
##                PC6         PC7         PC8         PC9
## SpCond -0.24626090  0.01484365  0.69135209 -0.23860450
## Temp    0.31736983  0.11528470 -0.19409288 -0.65631374
## DO     -0.17543755 -0.38813613 -0.08889209 -0.57845920
## pH      0.47681893 -0.07602463 -0.22961813  0.29014323
## Turb    0.31688328 -0.58116855  0.39028419  0.07782562
## Chla   -0.53367196  0.29780324 -0.08817841  0.08207387
## NH4    -0.13910680 -0.26781135 -0.19794148 -0.04453308
## NO23    0.41239471  0.55782809  0.36513120 -0.14272809
## PO4     0.07652751  0.14394777 -0.30188600 -0.24160832
write.csv(pca_nerr$rotation, 'dataout/swmp_pca_loadings_spc.csv')
pca_nerr$x
##                 PC1           PC2          PC3          PC4          PC5
##   [1,] -0.200402235  1.3716284766  1.156218215 -0.319546336  0.218042300
##   [2,] -0.101213563  1.3688465409  0.407624370 -0.435015947  0.474825539
##   [3,]  2.251106345  1.8307450578  0.576056899  1.093229014  1.122228614
##   [4,]  1.130268086  1.5347449068  0.749682819  0.097642905  0.688009239
##   [5,] -0.090838185  1.3742605387  1.092429822  0.042622516  0.136904731
##   [6,] -0.668846285  0.7542562318 -1.336204721 -0.719092436 -1.032696555
##   [7,] -0.633027479  0.7532834813 -1.675570901 -1.053931619 -0.570004705
##   [8,]  0.587244930  1.3408750890 -1.893257460 -0.420240900 -0.659889430
##   [9,]  0.456143686  1.2600239113 -2.019446114 -0.066713981 -0.568200924
##  [10,] -1.556844449  0.8599200287 -1.565632267 -0.822383041 -0.275651020
##  [11,]  2.231707491 -0.9245124975  0.322147025  0.847785325  0.380157846
##  [12,]  0.993945443 -0.4138394685 -1.277215629  0.385101977  1.657726094
##  [13,]  2.996807469  0.4093413434 -0.620555242  1.530779719  0.532089229
##  [14,]  2.578889729  0.0126943829 -1.720134459 -1.638732511 -0.643327845
##  [15,]  2.972013063 -0.4733720203 -0.427812743  0.211414585  0.056120173
##  [16,]  0.202135191  0.1437396170 -0.299160519 -0.861173464  0.103111402
##  [17,] -1.317250949 -0.6364755584 -0.559372478 -0.820208903 -0.499045491
##  [18,]  2.504200708  0.0420175524 -0.668102102  0.396502786  0.749849857
##  [19,]  2.650104405 -0.0333424818 -0.418241010  0.299140842  0.611537175
##  [20,]  1.779428554  0.6287036473  0.230970450 -0.689960289  0.572103437
##  [21,]  3.500890689  0.2477542409 -0.757957832 -0.601802078  0.432152438
##  [22,]  3.191791717 -0.0782592771 -0.685929154  0.078244142 -0.006123801
##  [23,]  3.763380186  0.0760233949  0.058890355 -0.158826102 -0.172941476
##  [24,]  3.280975587  0.4972977652  1.043869017  0.148998216 -0.169150158
##  [25,] -0.682143036  0.3024112143  1.779002581 -0.112699864 -0.125511431
##  [26,] -0.442781406  0.3122467814  1.715660920 -0.768751512 -0.602418359
##  [27,] -0.361898273 -0.2507669022  1.672341817 -0.425375281 -0.531479672
##  [28,] -0.469112447 -1.2469059008  1.548993174 -0.348276211 -0.638444749
##  [29,] -0.403608782  1.4954651152 -1.565020771  0.488041905  0.702323319
##  [30,]  0.008793802  2.4950773359 -0.743109306  1.863872253  0.066027246
##  [31,] -1.084534932  0.9989652274 -1.734490439 -0.465565086  1.035095218
##  [32,] -1.473759318  0.8508444123 -1.599950598 -1.280485174  0.811326546
##  [33,] -0.450560532 -1.1217288344  0.372265258 -0.712758740 -0.029546294
##  [34,]  0.668614015 -1.1084910135 -0.523134638  0.920650583 -0.353576626
##  [35,]  0.570130570 -0.5241760111  0.974704554 -0.311168462 -0.195713944
##  [36,]  1.239618709 -0.2009062917  1.255586508 -0.407288408 -0.754204112
##  [37,] -1.444502463  1.1671233889  0.372951792 -0.252464884 -0.349625437
##  [38,]  0.113926855  1.9020131198  0.180164217  0.598238574  0.588820774
##  [39,] -0.446348680  1.6793025968  0.469757162  0.265813800  0.088469670
##  [40,] -0.995893112  1.3872279768  0.483224963 -0.240095207 -0.281683631
##  [41,] -3.027964976  0.2234141996 -0.721362143  0.801235308 -0.633435456
##  [42,] -3.835626745  0.0137166576 -1.624765921  0.697734421  0.794234857
##  [43,] -0.158808712  2.6395082069 -1.088743734  3.418297230  1.917929516
##  [44,]  1.977975795 -2.1824657315  0.103368017 -0.210896570  0.337608506
##  [45,]  0.592960851 -2.2460243461  0.145075515  0.415554334 -0.071304835
##  [46,] -0.311428188 -3.2497037510 -1.471969521 -0.287988139 -0.461674468
##  [47,]  1.620985986 -1.1036946924 -0.826206821  0.280418832 -0.198529765
##  [48,]  1.373952902 -1.3364978751 -0.848868152  0.160140710 -0.184280539
##  [49,] -1.093629908 -0.3834892787  0.291625481 -0.974116310  0.744920454
##  [50,] -0.969655799 -0.2534052668  0.190459330 -0.807629787  0.729378247
##  [51,]  2.327756907 -0.8003908611 -1.879419408  2.266133651  1.188670257
##  [52,]  0.773992992 -0.8162425813 -0.006019797  0.479491312  0.081344354
##  [53,] -2.225347032  2.4733204839 -0.154754492  1.223835897 -1.511682740
##  [54,] -2.783970485  1.5595089404 -0.735263984  1.555247813 -2.261474309
##  [55,] -3.589013613  0.7308212872 -0.932792928  1.447992394 -2.114486266
##  [56,] -3.377015091  0.7617413519 -0.693250289  1.705113946 -1.966007942
##  [57,] -1.098870382 -3.5338504324  1.081945910 -0.130526372  0.062650001
##  [58,] -1.068722248 -3.4291910363  0.497705539 -0.829821368  0.275441956
##  [59,] -1.432547065 -3.6603403021  0.898474939 -0.110579070  0.025737292
##  [60,] -1.293086085 -3.7749664712  0.804811104 -0.360103220 -0.079252269
##  [61,]  1.820782933 -1.1043111257 -1.015708140 -0.636520991 -0.663248537
##  [62,]  1.716814118 -1.0308810119 -0.411319244 -0.137532040 -1.434833366
##  [63,]  0.687219935 -0.9388947705 -1.453922063 -0.119340969 -0.075809400
##  [64,]  1.738179687  0.8672086429 -0.545401703  0.369344054  1.213091293
##  [65,] -2.047181955  0.8432391422 -0.849468340 -1.376572855  1.507255639
##  [66,] -1.491216160  0.9825672954 -0.457046271 -1.268093769  1.789807668
##  [67,] -1.210164987  0.9596908541 -0.662321087 -1.439722041  2.219623981
##  [68,] -1.641186601  1.0075826426 -0.849343395 -1.597527957  1.887088237
##  [69,] -2.360956882  0.8693036050 -1.141307019 -0.985282761  0.843374489
##  [70,] -1.547947533 -0.5694738280  0.594717247  1.162334194  0.664706741
##  [71,] -2.054506895 -1.8437973242  0.421696017 -0.029493929  0.454390342
##  [72,] -2.450808989 -1.9074444487  0.350663902  0.033744764  0.480026709
##  [73,] -2.615856736 -1.8977266476  0.061879094  0.015686479  0.720067705
##  [74,] -0.698045232  1.2542467284  0.554445303  0.039682384 -0.326488311
##  [75,] -0.754891838  1.2400737082  0.429912888 -0.042880265 -0.310582498
##  [76,] -0.481223384  1.6568091126  0.571446610 -0.006497154 -0.192064821
##  [77,]  2.558238550  1.0424716569  0.427627827  0.486928124  0.108041894
##  [78,]  0.386470146  0.3471305811  0.256096038 -0.679098947 -0.536365658
##  [79,] -1.938679135  0.4597308466 -0.447786452  0.041224615 -0.940505437
##  [80,] -1.571049075  0.7421068599 -0.469273049 -0.004676726 -0.818362895
##  [81,]  0.698492042  0.9579087491  0.593217851 -0.622341584 -0.176788790
##  [82,]  1.624233260 -1.7798689279 -0.043418886  0.078660892 -0.134865204
##  [83,]  2.176853333 -0.6127418112 -0.173218811  0.170585917 -0.133478985
##  [84,]  2.893275800 -0.0806549771 -0.486302312 -1.253010841 -0.799826228
##  [85,]  2.416996108 -0.8357356176  0.098232297 -0.604840512 -0.526701751
##  [86,]  2.858120641 -0.0763196535 -0.439038875 -1.146151559 -0.866330538
##  [87,] -0.984400494 -2.7255460689  1.012165889 -0.460020248  0.315200326
##  [88,] -0.777211751 -2.5177786990  1.123760966 -0.245997442  0.458817505
##  [89,] -0.693563623 -2.0760260059  1.502783625  0.455165336  0.442330872
##  [90,] -1.169314595 -2.7905099635  0.947255306 -0.106219366  0.474282792
##  [91,]  1.732591485 -1.0815550088  1.901958309  0.175116312 -1.990578094
##  [92,]  3.649191948 -0.0991618026  1.231637170  1.956854580 -1.204831994
##  [93,] -1.244916810  2.1504356005  0.024574979  0.142930733 -0.054188461
##  [94,] -1.002282591  1.8278480814 -0.020435420  0.770764334 -0.358235807
##  [95,] -1.568654689  1.9758843644 -0.251618881 -0.006462161 -0.226801785
##  [96,] -0.877969904  2.3379779556  0.773824321  0.560543518 -0.053605710
##  [97,] -1.182250463  2.2478339755  0.330734031  0.257924885  0.182840392
##  [98,] -0.483619842  1.7425300497  1.185686907 -0.693910238  0.514012127
##  [99,]  0.845615004  2.0875022681  2.817962762  0.924274794 -0.049245679
## [100,] -0.048343429  2.1371767093  0.322268437  0.811767158  0.894031818
## [101,] -0.587368078  1.8086869519  0.580721312  0.836763536  0.550413107
## [102,]  0.250655950  1.7188864664  0.596870964 -0.638490781  0.403688528
## [103,]  0.279269891  1.2068536914  0.511648116 -0.718464790  0.400509068
## [104,]  1.438860748 -0.0007382128  1.482703865 -1.156823833  0.578599427
## [105,]  2.720853435 -0.0579177824  1.687304620 -0.216494783  0.663302905
## [106,]  1.494884432  0.3796501178  1.830187439 -0.608209909  0.480096061
## [107,]  2.377565841 -0.2199104730  0.796701983 -0.010000666  0.563415423
## [108,] -0.712774538 -2.0242653299  1.075755760 -0.451663010 -0.249650202
## [109,] -0.371311352 -1.6944390827  0.886390077 -0.397624467 -0.256006594
## [110,]  0.655317902 -1.8903871660 -0.393545938  1.207170902  0.015825133
## [111,] -1.191217704 -0.4490428537  2.438682619  0.630712273 -0.548877125
## [112,] -0.294420241  0.7746409245  2.517613173  1.088253255 -0.619797861
## [113,] -0.504620967  0.7169046667  2.745025510  1.017599068 -0.133502686
## [114,] -2.037601031  0.8989471892  0.397771498 -2.229720409  0.303929686
## [115,] -1.392618397  0.7707606542  0.935645870 -0.113985431  0.233852377
## [116,] -1.522184879  0.5081826534  1.006476515 -0.095942061  0.416729401
## [117,]  0.243056712  1.3522783207  2.871464992  1.025608844 -0.088930048
## [118,] -0.242637192 -3.8072064452 -3.520601285  3.013532431  0.826966318
## [119,] -1.234472058 -2.5472987285  0.584276784  0.300969308 -0.268877907
## [120,] -1.051500395 -2.3530117797 -0.837842678  0.778126484 -0.598463442
## [121,] -0.385992363 -2.7746617263 -3.165889291  1.989589410  0.771261839
## [122,]  1.993994069  1.0443822850 -2.202013244 -0.060165184 -1.467970112
## [123,]  0.893527003  0.9821496223 -2.441104843 -1.835751697 -1.144161720
## [124,]  1.452017254  1.2269680811 -2.254531908 -1.364491169 -1.422601647
## [125,]  0.732746280  1.1219639011 -2.305809670 -1.760925100 -1.203000518
## [126,] -0.499559924 -0.5571001065 -0.282048563 -1.005055174 -0.524935227
## [127,] -1.873865792 -1.6720270140  0.144223833 -0.363595015  0.253052017
## [128,] -2.360899858 -1.1953656281 -0.355591662 -0.178246210 -0.170433028
## [129,] -2.427026348 -0.7525405535 -0.550395858  0.250927746 -0.303155786
##                  PC6          PC7          PC8          PC9
##   [1,]  0.2392498507 -0.431464195  0.243083289 -0.143778968
##   [2,]  0.0930351866  0.064082296  0.474974308 -0.043481087
##   [3,] -0.3297019194  0.203855434 -0.274567373 -0.050483163
##   [4,]  0.0092686648  0.015856104  0.004064807 -0.102713767
##   [5,]  0.2153417873 -0.353444892  0.219941728 -0.120149893
##   [6,]  0.3699533749  0.075365616  0.296060158  0.122846968
##   [7,]  0.3269294711  0.065908657  0.372795039  0.122537436
##   [8,] -0.2110245161 -0.136086896 -0.248752470  0.091201448
##   [9,] -0.3678170434  0.290597114 -0.379704108  0.128224046
##  [10,]  0.3247920944 -0.066753603  0.420484635  0.458473364
##  [11,]  0.2466552490  0.085466546 -0.586730686 -0.410147482
##  [12,] -1.0403413482  0.480448719  0.374139656 -0.019014646
##  [13,] -0.7604364704  0.337967478  0.140801807 -0.066748291
##  [14,] -0.0863594906 -0.016400593 -0.140875914 -0.284545975
##  [15,] -0.2597606938  0.104099570 -0.009171909 -0.114953452
##  [16,] -0.4826314145 -0.610303794 -0.307319299  0.197642123
##  [17,] -0.8238042703 -1.180281514 -0.931727079  0.208814590
##  [18,]  0.0835278239 -0.307791962 -0.003715411 -0.210905512
##  [19,] -0.0467292310 -0.368538423 -0.151252782 -0.387197932
##  [20,] -0.2357731431 -0.765922520 -0.021753087  0.091016736
##  [21,] -0.9080652565  0.016549022  0.220428858 -0.116778346
##  [22,] -1.7583953440  0.520258350 -0.480681106 -0.429661589
##  [23,] -0.6177556203  0.066548837  0.128470499 -0.271833568
##  [24,] -0.3626023411  0.027936473  0.422007870  0.083213092
##  [25,] -0.2363021838  0.273025291 -0.083871364  0.342036656
##  [26,]  0.0245055087  0.392842305  0.001067156  0.414587306
##  [27,]  0.1411209072  0.198831194  0.189257985  0.164451241
##  [28,]  0.0782455331  0.169054972  0.252350249 -0.004879045
##  [29,] -0.6987440644  0.187157676  0.010710289 -0.004550123
##  [30,] -0.9154359690  0.588713567  0.158470008  0.754220748
##  [31,] -0.1552617317 -0.068600012  0.184566469  0.023598479
##  [32,]  0.3262028363 -0.346872756  0.067777942  0.132888248
##  [33,] -0.3998936109  0.139609643 -0.082952314 -0.062177706
##  [34,] -0.2534683819 -0.112144925 -0.032414098 -0.565775018
##  [35,] -0.1448822629  0.003430294 -0.005985788 -0.232031620
##  [36,] -0.1115730876 -0.391140899 -0.099628518 -0.220107936
##  [37,]  0.2746789109 -0.136563793  0.084489562 -0.172922177
##  [38,] -0.2716641949  0.426367926 -0.627291885 -0.082411245
##  [39,] -0.0650790848 -0.018433397  0.060103306 -0.277590821
##  [40,]  0.2802813038  0.042763696  0.267851038 -0.177241128
##  [41,]  0.9903786349  0.119603602  0.059365786 -0.349507591
##  [42,]  1.2413789743  0.433232550  0.420356174 -0.224145026
##  [43,]  0.7048557087  0.052311898 -0.537627597  1.277060636
##  [44,]  1.2860802893 -0.082911395 -0.162389145 -0.203109278
##  [45,]  1.3950750935  0.125959302 -1.104437000 -0.222371692
##  [46,]  1.5641330689  0.472775136 -0.795967616 -0.048462006
##  [47,]  0.5431145699 -0.171251346 -0.362538921 -0.095115785
##  [48,]  0.6445203383 -0.097395193 -0.461212843 -0.061114764
##  [49,] -0.4485934288 -0.808374372  0.030870783  0.199614089
##  [50,] -0.5390239381 -0.401496407 -0.050106311  0.084900374
##  [51,] -1.0015808045 -0.127722538  0.697821830 -0.682471650
##  [52,] -0.8160710080 -0.155745862 -0.117644982 -0.321917643
##  [53,] -0.3666827957  0.580879640 -0.175277198 -0.096449171
##  [54,] -0.0818333791  0.517384290 -0.165851871 -0.342431659
##  [55,]  0.5812070018 -0.237558775 -0.371068372 -0.455760605
##  [56,]  1.1521921126 -0.762264542 -0.070057528 -0.433412906
##  [57,] -0.2107213676 -0.208725761  0.397899621  0.165854092
##  [58,] -0.0813149023 -0.515793955  0.320914079 -0.246026509
##  [59,] -0.4108050584  0.391909616  0.213276975  0.101437682
##  [60,] -0.6427138804  0.280055612  0.130122481 -0.024823999
##  [61,] -0.3299351779  0.049103220 -0.616403135  0.143983021
##  [62,]  0.0969336915 -0.485416847 -0.917787378  0.004065495
##  [63,]  0.3180959780 -0.211413159 -0.799794549  0.027767866
##  [64,]  0.5203375563 -0.880039988 -0.128112884  0.988547823
##  [65,]  0.6006642660  0.170412457  0.055793989 -0.088719728
##  [66,]  0.6519682665  0.096914484 -0.392543412 -0.365221745
##  [67,]  0.8486719776 -0.168738914 -0.249780677 -0.180333801
##  [68,]  0.8329573703 -0.133428901  0.081584658 -0.077202887
##  [69,]  0.6104804931  0.130047710  0.289910652  0.077874068
##  [70,] -0.9870894175 -0.052370758 -0.335478832  0.102207803
##  [71,] -1.2441672413  0.056851902 -0.659308206  0.210714116
##  [72,] -1.0373635880 -0.069617600 -0.548428617  0.451409063
##  [73,] -0.9259596761 -0.008872934 -0.514781883  0.376222366
##  [74,] -0.4806714451 -0.424620168  0.361942577  0.108782469
##  [75,] -0.5994892144 -0.392281149  0.270559201  0.052547648
##  [76,] -0.7598404011 -0.292912323  0.341996371  0.103525045
##  [77,] -0.1360408442 -0.184348349 -0.139487794 -0.394970617
##  [78,]  0.0346775450  0.104948143  0.082021124 -0.230369975
##  [79,] -0.3342552482 -0.963995687  0.097587075  0.258109143
##  [80,] -0.5384303559 -0.890647392  0.082360843  0.301592034
##  [81,] -0.0563657415  0.104850720  0.105609932 -0.100222689
##  [82,]  1.6239557813  0.466659100 -0.205317300  0.195790552
##  [83,]  1.2541423247  0.659001771  0.111934643  0.671430869
##  [84,]  0.3901378952  0.124671721 -0.083234777  0.443341630
##  [85,]  1.2059701150  0.600575985 -0.105598155  0.548866245
##  [86,]  0.4091838079  0.035320734 -0.102067547  0.477694702
##  [87,] -0.0862789273  0.302271704  0.006025155 -0.275812563
##  [88,] -0.0900433029  0.746234951  0.238018641 -0.007425653
##  [89,]  0.1791089101  1.185261227  0.501457598  0.516937117
##  [90,] -0.1654530017  1.273856514  0.070815768 -0.113420851
##  [91,] -0.1959503266 -0.654984708  0.721974180 -0.051482623
##  [92,] -0.0932412900 -0.379367131  0.367403945  0.938153940
##  [93,]  0.2142611072  0.169126983 -0.079940173 -0.117174646
##  [94,]  0.6307217743  0.379044936  0.101585570 -0.155057018
##  [95,]  0.3780769978  0.099039349  0.216846276 -0.091839421
##  [96,]  0.5580800269  0.447262590  0.278484394 -0.131816925
##  [97,]  0.4672611800  0.135636174 -0.017000552 -0.084374404
##  [98,] -0.5171184392 -0.661254728 -0.292953682 -0.243090381
##  [99,] -0.0700958499 -0.154662676 -0.415296187 -0.398627015
## [100,] -0.7037919648  0.492580677 -0.214130729 -0.369111135
## [101,] -0.6963295917  0.045432992 -0.355494676  0.008350379
## [102,] -0.1093075402 -0.316409482  0.284131500  0.054369594
## [103,]  0.0364358326 -0.021875265  0.210264629 -0.414195733
## [104,]  0.5771080352 -0.498795695  0.600636040 -0.209537111
## [105,]  1.3822507376  0.102837607 -0.301745224 -0.155782483
## [106,]  0.5129625786 -0.502233390  0.331532280 -0.108776138
## [107,]  1.0023656570 -0.240006067 -0.132828934 -0.140808724
## [108,] -0.0740488207  0.133813665  0.216851065 -0.049088869
## [109,] -0.1525464010  0.064872773  0.289401300 -0.012300609
## [110,]  0.1040341264 -0.314776326  0.709182614 -0.027464618
## [111,]  0.3459221986 -0.147885906 -0.525238791 -0.174584497
## [112,]  0.3461436254 -0.462793372  0.295832072  0.169626253
## [113,]  0.1990377465  0.306644588 -0.248957426  0.003301457
## [114,] -0.6849441153  0.748731152  0.387940456 -0.009710346
## [115,]  0.4789851038 -0.290454689  0.251462381  0.032963609
## [116,]  0.4769377292 -0.139210048  0.123201257 -0.134130214
## [117,]  0.5568338594  0.854690706  0.064946521  0.128384416
## [118,]  0.1167063560 -0.393587273  0.620452262 -0.520433582
## [119,] -0.2499983431 -0.720699482  0.482604381  0.254303757
## [120,]  0.2981387726 -0.510146306  0.492883910  0.254054080
## [121,]  0.6009306951 -0.289032488  0.776777268  0.186862716
## [122,] -0.4449584306  0.734541125  0.492492679 -0.207546157
## [123,] -0.0517613842  0.297079618  0.115591871  0.130199523
## [124,]  0.0005872656  0.522569798  0.359778541  0.024064282
## [125,] -0.2529304562  0.043739373  0.029471724  0.191360297
## [126,] -1.1701690753  1.191552982  0.066937436  0.079912269
## [127,] -0.7478348228 -0.248449075 -0.138836979  0.198553188
## [128,] -0.9103565477  0.193351617 -0.548310100  0.013560909
## [129,] -0.9581814591  0.164933125 -0.539375991  0.125784832
#pca_nerr <- prcomp(df.n, center=FALSE, scale=FALSE)
pca_smry<-summary(pca_nerr)
pca_smry
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5    PC6     PC7
## Standard deviation     1.7069 1.5262 1.2015 0.93117 0.79628 0.6342 0.43078
## Proportion of Variance 0.3237 0.2588 0.1604 0.09634 0.07045 0.0447 0.02062
## Cumulative Proportion  0.3237 0.5825 0.7429 0.83927 0.90973 0.9544 0.97504
##                            PC8     PC9
## Standard deviation     0.36606 0.30104
## Proportion of Variance 0.01489 0.01007
## Cumulative Proportion  0.98993 1.00000
# str(pca_smry)
pca_smry$importance
##                             PC1      PC2      PC3       PC4       PC5       PC6
## Standard deviation     1.706891 1.526237 1.201466 0.9311692 0.7962843 0.6342399
## Proportion of Variance 0.323720 0.258820 0.160390 0.0963400 0.0704500 0.0447000
## Cumulative Proportion  0.323720 0.582540 0.742930 0.8392700 0.9097300 0.9544200
##                              PC7       PC8       PC9
## Standard deviation     0.4307805 0.3660578 0.3010445
## Proportion of Variance 0.0206200 0.0148900 0.0100700
## Cumulative Proportion  0.9750400 0.9899300 1.0000000
nerr_col<-'#05829C'
fviz_eig(pca_nerr,barfill = nerr_col,xlab = 'Principle Components',
  barcolor = nerr_col,main='',)+   
            theme(axis.text=element_text(size=16),
            axis.title=element_text(size=18,face="bold"))

Kaiser Criterion helps to establish number of Principle Components that explain enough variance. According to the criterion, one should use as many PCs, as there is number of eigenvalues which are higher than 1. (3 PCs in this case)

eig.val<-get_eigenvalue(pca_nerr)
eig.val
##       eigenvalue variance.percent cumulative.variance.percent
## Dim.1 2.91347653        32.371961                    32.37196
## Dim.2 2.32939905        25.882212                    58.25417
## Dim.3 1.44352138        16.039126                    74.29330
## Dim.4 0.86707615         9.634179                    83.92748
## Dim.5 0.63406864         7.045207                    90.97269
## Dim.6 0.40226028         4.469559                    95.44224
## Dim.7 0.18557184         2.061909                    97.50415
## Dim.8 0.13399834         1.488870                    98.99302
## Dim.9 0.09062779         1.006975                   100.00000
for(i in 1:3){
  var<-get_pca_var(pca_nerr)
  print(var$contrib)
  plot(fviz_contrib(pca_nerr, "var", axes=i, xtickslab.rt=90,fill = nerr_col,
                    color = nerr_col)+   
            labs(title=paste("Contributions of variables to PC",i)) +
            theme(
              title=element_text(size=18),
              axis.text=element_text(size=16),
              axis.title=element_text(size=18,face="bold")), main = 'PC1')
}
##             Dim.1       Dim.2        Dim.3        Dim.4        Dim.5     Dim.6
## SpCond 22.6438487  1.11563881 13.626608410  2.010390827 1.027055e+00  6.064443
## Temp    1.9888702 32.97043526  5.419934874  0.000514019 1.376851e+00 10.072361
## DO      0.5143059 33.24993243  3.264744855 10.549425338 2.710774e-02  3.077833
## pH     16.4641734  0.77403204  1.923735704 38.351266220 5.482431e+00 22.735630
## Turb   18.9645721 10.43294555  0.007112147  4.696830513 6.243493e+00 10.041502
## Chla    8.5999673 10.51185267  1.209106280 40.878632290 3.333429e-05 28.480576
## NH4     9.2554616  1.72724313 22.062398070  1.896310052 5.183482e+01  1.935070
## NO23   20.3242757  9.17685797  0.142653677  1.136890294 5.725955e+00 17.006940
## PO4     1.2445251  0.04106214 52.343705983  0.479740447 2.828225e+01  0.585646
##              Dim.7      Dim.8      Dim.9
## SpCond  0.02203339 47.7967708  5.6932107
## Temp    1.32905630  3.7672044 43.0747726
## DO     15.06496564  0.7901804 33.4615044
## pH      0.57797449  5.2724487  8.4183092
## Turb   33.77568796 15.2321747  0.6056826
## Chla    8.86867715  0.7775432  0.6736121
## NH4     7.17229190  3.9180828  0.1983195
## NO23   31.11721727 13.3320793  2.0371309
## PO4     2.07209591  9.1135158  5.8374579

##             Dim.1       Dim.2        Dim.3        Dim.4        Dim.5     Dim.6
## SpCond 22.6438487  1.11563881 13.626608410  2.010390827 1.027055e+00  6.064443
## Temp    1.9888702 32.97043526  5.419934874  0.000514019 1.376851e+00 10.072361
## DO      0.5143059 33.24993243  3.264744855 10.549425338 2.710774e-02  3.077833
## pH     16.4641734  0.77403204  1.923735704 38.351266220 5.482431e+00 22.735630
## Turb   18.9645721 10.43294555  0.007112147  4.696830513 6.243493e+00 10.041502
## Chla    8.5999673 10.51185267  1.209106280 40.878632290 3.333429e-05 28.480576
## NH4     9.2554616  1.72724313 22.062398070  1.896310052 5.183482e+01  1.935070
## NO23   20.3242757  9.17685797  0.142653677  1.136890294 5.725955e+00 17.006940
## PO4     1.2445251  0.04106214 52.343705983  0.479740447 2.828225e+01  0.585646
##              Dim.7      Dim.8      Dim.9
## SpCond  0.02203339 47.7967708  5.6932107
## Temp    1.32905630  3.7672044 43.0747726
## DO     15.06496564  0.7901804 33.4615044
## pH      0.57797449  5.2724487  8.4183092
## Turb   33.77568796 15.2321747  0.6056826
## Chla    8.86867715  0.7775432  0.6736121
## NH4     7.17229190  3.9180828  0.1983195
## NO23   31.11721727 13.3320793  2.0371309
## PO4     2.07209591  9.1135158  5.8374579

##             Dim.1       Dim.2        Dim.3        Dim.4        Dim.5     Dim.6
## SpCond 22.6438487  1.11563881 13.626608410  2.010390827 1.027055e+00  6.064443
## Temp    1.9888702 32.97043526  5.419934874  0.000514019 1.376851e+00 10.072361
## DO      0.5143059 33.24993243  3.264744855 10.549425338 2.710774e-02  3.077833
## pH     16.4641734  0.77403204  1.923735704 38.351266220 5.482431e+00 22.735630
## Turb   18.9645721 10.43294555  0.007112147  4.696830513 6.243493e+00 10.041502
## Chla    8.5999673 10.51185267  1.209106280 40.878632290 3.333429e-05 28.480576
## NH4     9.2554616  1.72724313 22.062398070  1.896310052 5.183482e+01  1.935070
## NO23   20.3242757  9.17685797  0.142653677  1.136890294 5.725955e+00 17.006940
## PO4     1.2445251  0.04106214 52.343705983  0.479740447 2.828225e+01  0.585646
##              Dim.7      Dim.8      Dim.9
## SpCond  0.02203339 47.7967708  5.6932107
## Temp    1.32905630  3.7672044 43.0747726
## DO     15.06496564  0.7901804 33.4615044
## pH      0.57797449  5.2724487  8.4183092
## Turb   33.77568796 15.2321747  0.6056826
## Chla    8.86867715  0.7775432  0.6736121
## NH4     7.17229190  3.9180828  0.1983195
## NO23   31.11721727 13.3320793  2.0371309
## PO4     2.07209591  9.1135158  5.8374579

v<-c(1:3)
v.pair<-combn(v, 2)
v.pair
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    2    3    3
class(summary(pca_nerr))
## [1] "summary.prcomp"
str(summary(pca_nerr))
## List of 6
##  $ sdev      : num [1:9] 1.707 1.526 1.201 0.931 0.796 ...
##  $ rotation  : num [1:9, 1:9] -0.4759 -0.141 0.0717 -0.4058 0.4355 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:9] "SpCond" "Temp" "DO" "pH" ...
##   .. ..$ : chr [1:9] "PC1" "PC2" "PC3" "PC4" ...
##  $ center    : Named num [1:9] 31.421 18.791 7.268 7.68 0.887 ...
##   ..- attr(*, "names")= chr [1:9] "SpCond" "Temp" "DO" "pH" ...
##  $ scale     : Named num [1:9] 19.775 5.224 1.469 0.379 0.436 ...
##   ..- attr(*, "names")= chr [1:9] "SpCond" "Temp" "DO" "pH" ...
##  $ x         : num [1:129, 1:9] -0.2004 -0.1012 2.2511 1.1303 -0.0908 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : NULL
##   .. ..$ : chr [1:9] "PC1" "PC2" "PC3" "PC4" ...
##  $ importance: num [1:3, 1:9] 1.707 0.324 0.324 1.526 0.259 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:3] "Standard deviation" "Proportion of Variance" "Cumulative Proportion"
##   .. ..$ : chr [1:9] "PC1" "PC2" "PC3" "PC4" ...
##  - attr(*, "class")= chr "summary.prcomp"
smry.pca_nerr<-summary(pca_nerr)
prop.var<-smry.pca_nerr$importance[2,]

for(c in 1:ncol(v.pair)){      
  pair<-v.pair[,c]
  xlbl<-paste0('PC',pair[1],' (',round(prop.var[pair[1]]*100,digits=1),'%)')
  ylbl<-paste0('PC',pair[2],' (',round(prop.var[pair[2]]*100,digits=1),'%)')
  pl<-fviz_pca_var(pca_nerr,axes=pair,
             col.var = "contrib", # Color by contributions to the PC
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE,     # Avoid text overlapping
             title = " ",
             labelsize = 10
             )+ coord_fixed() + xlab(xlbl) +  
             ylab(ylbl) +
             theme(axis.text=element_text(size=28),
             axis.title=element_text(size=32,face="bold"))
  print(pl)
}
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

v<-c(1:3)
v.pair<-combn(v, 2)
v.pair
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    2    3    3
for(c in 1:ncol(v.pair)){
  pair<-v.pair[,c]
  xlbl<-paste0('PC',pair[1],' (',round(prop.var[pair[1]]*100,digits=1),'%)')
  ylbl<-paste0('PC',pair[2],' (',round(prop.var[pair[2]]*100,digits=1),'%)')
  pl<-fviz_pca_var(pca_nerr,axes=pair,
             #col.var = "contrib", # Color by contributions to the PC
             #radient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE,     # Avoid text overlapping
             title = " ",
             labelsize = 10
             )+ coord_fixed() +  xlab(xlbl) +  
             ylab(ylbl) + 
            theme(axis.text=element_text(size=28),
            axis.title=element_text(size=32,face="bold"))
  print(pl)
}
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.

stn_ids<-read.csv('data/stn_map_ids.csv',stringsAsFactors = F)
nerr_h_comp.stn_ids.mrg<-merge(nerr_h_comp,stn_ids,by.x='code',by.y='code',all.x=T)

fviz_pca_biplot(pca_nerr, geom='point',
                col.var = "#2E9FDF", # Variables color
                col.ind = "#696969"  # Individuals color
                )+
  geom_text(aes(label=nerr_h_comp.stn_ids.mrg$Map.Code),hjust=1.2, vjust = 1.2,col='gray60')+ coord_fixed()

nerr_h_comp.stn_ids.pc.mrg<-cbind(nerr_h_comp.stn_ids.mrg,pca_nerr$x)
write.csv(nerr_h_comp.stn_ids.pc.mrg,'dataout/stn_med_pc_spc.csv')
pca_nerr_transform = as.data.frame(pca_nerr$x[,1:3])

How many clusters?

k.max <- 15 # we want to look at the within squares for clusters 2-15. 
wss <- sapply(1:k.max, function(k){kmeans(pca_nerr_transform, k, nstart = 50)$tot.withinss})
plot(1:k.max, wss, type = "b", pch = 19, frame = FALSE, #type = b indicates points joined by lines
     xlab = "Number of CLusters K",  #pch is the shape of the point, 19 = circle 
     ylab = "Total Within Clusters Sum of Squares")

fviz_nbclust(pca_nerr_transform, kmeans, method = 'wss')

fviz_nbclust(pca_nerr_transform, kmeans, method = 'silhouette')

#fviz_nbclust(pca_nerr_transform, kmeans, method = 'gap_stat')

Although unsupervised metrics such as silhouette width suggested setting the K number of clusters for this dataset to three, four clusters were selected to capture a fourth, generally eutrophic cluster characterized by higher phosphate than the other eutrophic cluster that was generally characterized by high nitrate + nitrite.

v<-c(1:3)
pc.pair<-combn(v, 2)

n_k<-4 # set 4 clusters

for (k in n_k){ #change to range if want to look at multiple clusters
  set.seed(42)
  kmeans_nerr = kmeans(pca_nerr_transform, centers = k, nstart = 50)
  pc.labs<-colnames(kmeans_nerr$centers)
  clstr.lab<-LETTERS[1:k]
  clusters<-1:k
  #clust_clr<-brewer.pal(n = k, name = "Set2")
  
  kmeans_nerr$cluster[kmeans_nerr$cluster == 1]<-6
  kmeans_nerr$cluster[kmeans_nerr$cluster == 2]<-5
  kmeans_nerr$cluster[kmeans_nerr$cluster == 3]<-8
  kmeans_nerr$cluster[kmeans_nerr$cluster == 4]<-7
  
    
  kmeans_nerr$cluster[kmeans_nerr$cluster == 5]<-1 
  kmeans_nerr$cluster[kmeans_nerr$cluster == 6]<-3 
  
  
  kmeans_nerr$cluster[kmeans_nerr$cluster == 7]<-2 
  kmeans_nerr$cluster[kmeans_nerr$cluster == 8]<-4 
  
  
  clust_clr<-c("#66C2A5", "#FC8D62", "#8DA0CB",'#6E260E')
  clust_clr
  for(c in 1:ncol(pc.pair)){
    pair<-v.pair[,c]
    xlbl<-paste0('PC',pair[1],' (',round(prop.var[pair[1]]*100,digits=1),'%)')
    ylbl<-paste0('PC',pair[2],' (',round(prop.var[pair[2]]*100,digits=1),'%)')
    pc.lab<-pc.labs[pair]
    p<-fviz_cluster(kmeans_nerr, data = pca_nerr_transform[,pair], geom='point',stand=F, choose.vars=pc.lab,
      ggtheme = theme_minimal(),  axes = pair) +
      geom_label_repel(size =5,aes(label = nerr_h_comp.stn_ids.mrg$Map.Number, colour=cluster),
                       max.overlaps = Inf,show.legend = F) + coord_fixed() +
      scale_colour_manual(values = clust_clr,labels=clstr.lab) +
      scale_fill_manual(values = clust_clr,labels=clstr.lab) +
      scale_shape_manual(values=c(16,17,15,18),labels=clstr.lab) +
      xlab(xlbl) +
      ylab(ylbl) +
      theme(axis.text=element_text(size=28),
      axis.title=element_text(size=32,face="bold"),
      legend.title=element_text(size=22), 
      legend.text=element_text(size=20))
    
    print(p)
    
  
  
  }
  #
  stn_groups<-data.frame(nerr_h_comp$code,kmeans_nerr$cluster) 
  
  
  
  #join clusters with station metadata
  colnames(stn_groups)<-c('code','cluster')
  
  stn_groups2<-merge(stn_groups,nerr_h_comp,by='code')
  
  head(stn_groups2)
  unique(stn_groups2$cluster)
  
  stn_groups2$cluster <- factor(stn_groups2$cluster, levels = clusters)
  levels(stn_groups2$cluster)<-clstr.lab
  for(prm in colnames(nerr_h_comp)[2:ncol(nerr_h_comp)]){
    # Basic box plot
    p <- ggplot(stn_groups2, aes_string(x='cluster', y=prm, fill='cluster')) + 
      geom_boxplot(alpha=.6) + scale_fill_manual(values=clust_clr)+ scale_x_discrete("cluster", drop = FALSE) +
      theme(axis.text=element_text(size=32),
            axis.title=element_text(size=34,face="bold"),
            legend.title=element_text(size=24), 
            legend.text=element_text(size=22))
    print(p)
    
  }
  data_for_map<-merge(stn_groups2,nerr_h_comp.stn_ids.mrg, on=station)
  write.csv(data_for_map,paste0('dataout/data_for_map_spc',k,'clusters_spc.csv'),row.names=F)
  data_for_swmp_synth_group<-merge(stn_groups2[,1:2],nerr_h_comp.stn_ids.pc.mrg, on=code)
  head(data_for_swmp_synth_group)
  data_for_swmp_synth_group
  
  data_for_swmp_synth_group  <-data_for_swmp_synth_group %>% dplyr::select("code","cluster","SpCond","Temp",
                                            "DO","pH","Turb","Chla","NH4","NO23","PO4",
                                            "PC1","PC2","PC3","PC4","PC5","PC6","PC7","PC8","PC9")
  write.csv(data_for_swmp_synth_group,'dataout/swmp_clstr_med_pc_stations_spc.csv')
}

## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.